<?php
/**
 * Created by PhpStorm.
 * User: chen
 * Date: 18-5-26
 * Time: 下午4:18
 */

namespace App\Model\Common;

use Validator;

class Authority extends BasePaginateModel
{

    public $table = 'nosplit_authority';
    public $primaryKey = 'id';
    protected $hidden = ['created_at', 'updated_at'];
    public $errors = null;
    const TYPE_API = 1; // 接口类型
    const TYPE_ELEMENT = 2; // 页面元素类型

    public function isValid($params = null){
        if(empty($params)){
            $params = $this->toArray();
        }
        $except = '';
        if(!empty($this->id)){
            $except = ',id,'.$this->id;
        }
        $valid = Validator::make($params, [
            'name'             => 'required|unique:'.$this->table. $except,
            'module_id'        => 'required',
            'type'             => 'required'
        ], [
            'name.required' => '权限名称必填',
            'name.unique' => '权限名称已存在',
            'module_id.required' => '请选择模块',
            'type.required' => '权限类型必填'
        ]);
        $this->errors = $valid->messages();
        return !$valid->fails();
    }

}